package com.atmaowei.controller;

import com.atmaowei.common.Result;
import com.atmaowei.entity.Login;
import com.atmaowei.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/studentLogin")
    public Result studentLogin(@RequestBody Login login) {
        Login l = loginService.studentLogin(login);
        if (l != null) {
            return Result.success(l);
        }
        return Result.error();
    }

    @PostMapping("/teacherLogin")
    public Result teacherLogin(@RequestBody Login login) {
        Login l = loginService.teacherLogin(login);
        if (l != null) {
            return Result.success(l);
        }
        return Result.error();
    }

    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestBody Login login) {
        Login l = loginService.adminLogin(login);
        if (l != null) {
            return Result.success(l);
        }
        return Result.error();
    }

    @PostMapping("/register")
    public Result register(@RequestBody Login login) {
        if (loginService.register(login)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/findPassword")
    public Result findPassword(@RequestBody Login l) {
        Login login = loginService.findPassword(l);
        if (login!=null) {
            return Result.success(login.getPassword());
        }
        return Result.error();
    }

    @GetMapping("/checkStudentStatus/{sno}")
    public Result checkStudentStatus(@PathVariable String sno){
        return Result.success(loginService.checkStudentStatus(sno));
    }

    @GetMapping("/checkTeacherStatus/{tno}")
    public Result checkTeacherStatus(@PathVariable String tno){
        return Result.success(loginService.checkTeacherStatus(tno));
    }
}
